package com.bigstar.lolen_e;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class ServerStatus extends Activity{
	private ProgressDialog dialog;
	private boolean errer;
	private String na;
	private String euw;
	private String eune;
    @Override
    protected void onResume() {
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
    	super.onResume();
    }
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
		setContentView(R.layout.server_status);
        dialog = new ProgressDialog(this);        
        dialog.setCancelable(true);       
        dialog.setMessage("Loading the Server Status...");        
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);        
        dialog.setProgress(0);        
        dialog.setMax(100);        
        dialog.show();
        Thread myThread = new Thread(new Runnable() {     
        	public void run() {   
        		try {    
        			na = process("http://na.leagueoflegends.com/service-status");
            		dialog.setProgress(33);    
        			euw = process("http://euw.leagueoflegends.com/service-status");
            		dialog.setProgress(66);  
        			eune = process("http://eune.leagueoflegends.com/service-status");
            		dialog.setProgress(99);  
        			dialog.dismiss();
        		}catch (IOException e) {
        			// TODO Auto-generated catch block
        			errer = true;
        			e.printStackTrace();
        		}            
        		dialog.setProgress(100);    
        		dialog.dismiss();  
                new Thread(new Runnable() {
                    @Override
                    public void run() {    
                        runOnUiThread(new Runnable(){
                            @Override
                             public void run() {
                            	update();
                             }
                        });
                    }
                }).start();
        	}    
        });     
        myThread.start();
	}
	
    public String process(String url)throws IOException{
    	Source source = new Source(new URL(url));
    	List<Element> atags = source.getAllElements(HTMLElementName.SPAN);
    	Element e = null;
    	String txt = null;
    	for(int i = 0; i < atags.size(); i++){
        	e = (Element) atags.get(i);
        	txt = e.getContent().toString();
        	if(txt.contains("Online")){
            	return "Online";
        	}
        	if(txt.contains("busy")){
            	return "busy";
        	}
    	}
    	return "x";
    }

	public void update(){
    	if(errer){
    	}else{
	    	TextView tv = (TextView)findViewById(R.id.server_status_text);
        	tv.setText("Server Status");
        	TextView button = (TextView)findViewById(R.id.server_status_na);
        	if(na.contains("Online")){
        		button.setBackgroundResource(R.drawable.server_status_0);
        		button.setText("           North America : Online");
        	}else if(na.contains("busy")){
        		button.setBackgroundResource(R.drawable.server_status_1);
        		button.setText("           North America : Busy");
        	}else{
        		button.setBackgroundResource(R.drawable.server_status_2);
        		button.setText("           North America : Offline");
        	}
        	
        	
        	
        	
        	
        	button = (TextView)findViewById(R.id.server_status_euw);
        	if(euw.contains("Online")){
        		button.setBackgroundResource(R.drawable.server_status_0);
        		button.setText("  EU West : Online");
        	}else if(euw.contains("busy")){
        		button.setBackgroundResource(R.drawable.server_status_1);
        		button.setText("  EU West : Busy");
        	}else{
        		button.setBackgroundResource(R.drawable.server_status_2);
        		button.setText("  EU West : Offline");
        	}
        	
        	
        	
        	
        	
        	
        	
        	
        	
        	button = (TextView)findViewById(R.id.server_status_eune);
        	if(eune.contains("Online")){
        		button.setBackgroundResource(R.drawable.server_status_0);
        		button.setText("              EU Nordic & East : Online");
        	}else if(eune.contains("busy")){
        		button.setBackgroundResource(R.drawable.server_status_1);
        		button.setText("              EU Nordic & East : Busy");
        	}else{
        		button.setBackgroundResource(R.drawable.server_status_2);
        		button.setText("              EU Nordic & East : Offline");
        	}
        	
        	
        	
        	
    	}   
	}
}
